<template>
	<view class="page-fade-in" :style="{opacity: pageOpc}">
		<template v-if="pageOpc">
			<view class="content">
				<view class="shop-info-box">
					<view class="info-item flexWrapNo">
						<view class="item-title">金额上限:</view>
						<input placeholder="请输入随机立减券上限金额" placeholder-class="placeholderclass"  v-model="random_coupon_ceiling_money" type="digit"/>
					</view>
					<view class="info-item flexWrapNo">
						<view class="item-title">领券数量:</view>
						<input placeholder="请输入随机立减券领券数量密码" placeholder-class="placeholderclass" disabled  v-model="random_coupon_receive_num" type="number"/>
					</view>
					<view class="info-item flexWrap">
						<view class="flexWrapNo" style="flex: 1;">
							<view>有效时间：</view>
							<picker @change="bindPickerChange" :value="index" :range="array">
								<view class="uni-input">{{array[index]}}</view>
							</picker>
						</view>
						<text class="icon-more"></text>
					</view>
					<block v-if="index==1">
						<view class="info-item flexWrap">
							<view class="flexWrapNo" style="flex: 1;">
								<view>开始时间：</view>
								<picker mode="date" :data-index='0' :value="starTime" @change="bindDateChange" :start="date">
									<view class="select-input" v-if="starTime">{{starTime}}</view>
									<view class="select-input" v-else>请选择开始时间</view>
								</picker>
							</view>
							<text class="icon-more"></text>
						</view>
						<view class="info-item flexWrap">
							<view class="flexWrapNo" style="flex: 1;">
								<view>结束时间：</view>
								<picker mode="date" :data-index='1' :value="date" @change="bindDateChange" :start="starTime">
									<view class="select-input" v-if="endTime">{{endTime}}</view>
									<view class="select-input" v-else>请选择结束时间</view>
								</picker>
							</view>
							<text class="icon-more"></text>
						</view>
					</block>
					<view class="info-item flexWrap" v-if="index==2">
						<view class="flexWrapNo" style="flex: 1;">
							<view>有效天数：</view>
							<input placeholder="请输入天数" placeholder-class="placeholderclass" v-model="valid_configure" type="number" maxlength="5"/>
						</view>
						<text>天</text>
					</view>
				</view>
				<view class="bottom-box flexWrapNo">
					<view class="define-btn" @tap="setRandomCoupon()">确定</view>
				</view>
			</view>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			const starDate = this.getDate('start')
			return {
				pageOpc:0,
				random_coupon_ceiling_money:"",
				random_coupon_receive_num:1,
				array: ['永久有效', '指定日期', '指定天数'],
				index: 0,
				starTime: starDate,
				endTime: currentDate,
				date: currentDate,
				valid_configure:"",
				couponInfo:{}
			}
		},
		onLoad() {
			this.getRandomCoupon();
		},
		onReady(){
			setTimeout(() => {
				this.pageOpc=1
			}, 100)
		},
		methods: {
			getRandomCoupon(){
				this.$request.post(this.$api.getRandomCoupon,{
					store_id: uni.getStorageSync('store_id')
				}, {
					token:true
				}).then(res => {
					let {
						data = {}
					} = res;
					if (res.code==1) {
						this.couponInfo=data
						if(data.valid_time_data==2){
							data.valid_configure = data.valid_configure.replace(/-/g,"/");
							let customTimeArr=data.valid_configure.split(' / ')
							let start=customTimeArr[0]
							let end=customTimeArr[1]
							data.start=this.$tools.formatDay(start)
							data.end=this.$tools.formatDay(end)
							this.starTime=data.start
							this.endTime=data.end
						}else{
							this.valid_configure=data.valid_configure
						}
						this.index=data.valid_time_data?Number(data.valid_time_data-1):0
						this.random_coupon_ceiling_money=data.random_coupon_ceiling_money
						this.random_coupon_receive_num=1
					}
				})
			},
			setRandomCoupon(){
				this.$request.post(this.$api.setRandomCoupon,{
					store_id: uni.getStorageSync('store_id'),
					random_coupon_ceiling_money:this.random_coupon_ceiling_money,
					random_coupon_receive_num:this.random_coupon_receive_num,
					valid_timedata:Number(this.index)+1,
					valid_configure:this.index==2?this.valid_configure:this.starTime+' 00:00:00 - '+this.endTime+' 23:59:59',
				}, {
					token:true
				}).then(res => {
					let {
						data = {}
					} = res;
					if (res.code==1) {
						this.$tools.showToast('修改成功')
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},2000)
					}
				})
			},
			// 获取时间
			getDate(type) {
				const date = new Date();
				date.setDate(date.getDate());
				// if (type === 'start') {
				// 	date.setDate(date.getDate());
				// }
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			bindPickerChange: function(e) {
				this.index = e.detail.value
				if(this.index==2){
					this.valid_configure=""
				}else{
					this.valid_configure=this.couponInfo.valid_configure
				}
			},
			// 选择时间
			bindDateChange(e) {
				let index = e.currentTarget.dataset.index
				if (index == 0) {
					this.starTime = e.target.value
				} else {
					this.endTime = e.target.value
				}
			},
		}
	}
</script>

<style lang="scss">
	.content {
		padding: 0 36upx;
	
		.shop-info-box {
			.info-item {
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: 400;
				line-height: 40upx;
				color: #FFFFFF;
				padding: 20upx 0;
				border-bottom: 1upx solid #36355E;
				picker{
					flex: 1;
				}
				.icon-more{
					border-color: #FFFFFF;
				}
				.item-title {
					margin-right: 12upx;
					flex: none;
					font-size: 28rpx;
					width: 180upx;
				}
	
				.input-box {
					height: 100upx;
					width: 500upx;
					background:#1D1C5C;
					border-radius: 16upx;
					padding: 0 32upx;
					margin-top: 14upx;
					color: #fff;
					.placeholderClass {
						font-size: 32upx;
						font-family: PingFang SC;
						font-weight: 400;
						color: rgba(255, 255, 255, .5);
					}
				}
	
				input {
					font-size: 28upx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #fff;
				}
	
				.placeholderclass {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
				}
			}
		}
	
		.map-box {
			height: 600upx;
			margin: 20upx 0;
	
			map {
				width: 100%;
				height: 100%;
			}
		}
	
		.exeplan-box {
			font-size: 28upx;
			color: #FFFFFF;
			line-height: 46upx;
	
			.exeplan-title {
				font-size: 32upx;
				margin-bottom: 20upx;
			}
		}
	
		.bottom-box {
			height: 120rpx;
			background: #1D1C5C;
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			margin: auto;
			justify-content: center;
			font-size: 32rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			.define-btn {
				font-size: 32rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 80rpx;
				width: 322rpx;
				height: 80rpx;
				background: #26BCFD;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				text-align: center;
			}
		}
	}
</style>
